package com.luweijie.aop;



import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;

/**
 * 系统日志切点类
 * @author linrx
 *
 */
@Aspect
@Component
public class SystemLogAspect {


	@Autowired(required=false)
	private HttpServletRequest request;
	

     @Autowired
	private LogService logService;

	/**
	 * Service层切点 
	 */
/*	@Pointcut("@annotation(com.myron.ims.annotation.SystemServiceLog)")
	public void serviceAspect(){}*/
	
	/**
	 * Controller层切点 注解拦截
	 */
	@Pointcut("@annotation(com.luweijie.aop.SystemControllerLog)")
	public void controllerAspect(){}
	
	/**
	 * 方法规则拦截
	 */
	@Pointcut("execution(* com.luweijie.controller.*.*(..))")
	public void controllerPointerCut(){}
	/**
	 * 前置通知 用于拦截Controller层记录用户的操作的开始时间
	 * @param joinPoint 切点
	 * @throws InterruptedException 
	 */
	@Before("controllerAspect()")
	public void doBefore(JoinPoint joinPoint) throws InterruptedException{

	/*	if (name.equals("add")) {
			log.setOperationName((String) user);
			log.setOrderType("进行了检查添加");
			logService.updateLog(log);

		}
		if (name.equals("edit")) {
			log.setOperationName((String) user);
			log.setOrderType("进行了检查项修改");
			logService.updateLog(log);

		}*/
	}
	
	/**
	 * 后置通知 用于拦截Controller层记录用户的操作
	 * @param joinPoint 切点
	 */
	@SuppressWarnings("unchecked")
	@After("controllerAspect()")
	public void doAfter(JoinPoint joinPoint) {

		HttpSession session = request.getSession();
		Object user = session.getAttribute("user");

		Log log=new Log();
		String OrderType;

		OrderType=getControllerMethodDescription2(joinPoint);
		System.out.println("操作 " + OrderType);
		String name = joinPoint.getSignature().getName();
		if (name==null&&OrderType.equals("")) {
			return;
		}

		log.setOperationName((String) user);
		log.setOrderType(OrderType);
	/*	logService.updateLog(log);*/
 
	}
	
	/**
	 *  异常通知 
	 * @param joinPoint
	 * @param e
	 */
	@AfterThrowing(pointcut = "controllerAspect()", throwing = "e")
	public  void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
		Log log=new Log();
		if(log != null){
			log.setOrderType("error");
	/*		logService.updateLog(log);*/
		}
	}


	/**
	 * 获取注解中对方法的描述信息 用于Controller层注解
	 *
	 * @param joinPoint 切点
	 * @return 方法描述
	 */
	public static String getControllerMethodDescription2(JoinPoint joinPoint) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		SystemControllerLog controllerLog = method
				.getAnnotation(SystemControllerLog.class);
		String discription = controllerLog.description();
		return discription;
	}
	/**
	 * 日志更新线程
	 * 
	 * @author lin.r.x
	 *
	 */
	private static class UpdateLogThread extends Thread {
		private Log log;
		private LogService logService;

		public UpdateLogThread(Log log, LogService logService) {
			super(UpdateLogThread.class.getSimpleName());
			this.log = log;
			this.logService = logService;
		}

		@Override
		public void run() {
			/*this.logService.updateLog(log)*/;
		}
	}
}